<template>
  <el-form :model="ruleForm" :rules="rules" ref="validateForm" class="login-ruleForm">
    <el-form-item prop="username">
      <el-input
        :placeholder="t('login.username')"
        style="height: 40px; margin-top: 20px"
        v-model="ruleForm.account"
      >
        <template #prefix>
          <icon-user theme="outline" size="16" fill="#999" />
        </template>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        @keyup.enter="handleLogin"
        :placeholder="t('login.password')"
        type="password"
        v-model="ruleForm.pass"
        style="height: 40px; margin-top: 20px"
      >
        <template #prefix>
          <icon-lock theme="outline" size="16" fill="#999" />
        </template>
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        style="margin-top: 20px; border-radius: 10px"
        :loading="loading"
        class="login-btn"
        round
        @click="handleLogin"
        >{{ t('login.loginBtn') }}</el-button
      >
    </el-form-item>
    <el-form-item> </el-form-item>
  </el-form>
</template>

<script>
  import { reactive, toRefs, ref, unref, watch } from 'vue';
  import { useStore } from 'vuex';
  import { useRouter } from 'vue-router';
  import { useI18n } from 'vue-i18n';
  export default {
    setup() {
      const { t } = useI18n();
      const store = useStore();
      const router = useRouter();
      const validateForm = ref(null);
      const state = reactive({
        ruleForm: {
          account: '',
          pass: '',
        },
        loading: false,
        checkedPwd: false,
        redirect: undefined,
        rules: {
          account: [{ required: true, message: t('login.rules.username'), trigger: 'blur' }],
          pass: [{ required: true, message: t('login.rules.password'), trigger: 'blur' }],
        },
      });

      watch(
        () => router.currentRoute,
        ({ _value }) => {
          const route = _value;
          state.redirect = (route.query && route.query.redirect) || '/';
        },
        {
          immediate: true,
        }
      );

      const handleLogin = async () => {
        const form = unref(validateForm);
        if (!form) return;
        await form.validate((valid) => {
          if (valid) {
            state.valid = true;
            state.loading = true;
            store
              .dispatch('user/login', state.ruleForm)
              .then(() => {
                const routerPath =
                  state.redirect === '/404' || state.redirect === '/401' ? '/' : state.redirect;
                let id = JSON.parse(localStorage.getItem('data'));
                let type = id.data.name;
                if (type == '超级管理员') {
                  ElMessage({
                    showClose: true,
                    message: '欢迎至高无上的超级管理员!',
                    type: 'success',
                  });
                } else {
                  ElMessage({
                    showClose: true,
                    message: '欢迎' + type + '!',
                    type: 'success',
                  });
                }
                router.push(routerPath).catch(() => {});
                state.loading = false;
              })
              .catch(() => {
                state.loading = false;
              });
          }
        });
      };
      return {
        ...toRefs(state),
        validateForm,
        handleLogin,
        t,
      };
    },
  };
</script>
<style lang="scss" scoped>
  .mima {
    color: blue;
  }

  .login-ruleForm {
    margin-top: 1rem;

    :deep(.el-input__prefix) {
      top: 2px;
      padding: 0 4px;
    }

    .login-methods {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-around;
    }

    .login-btn {
      width: 100%;
      background-color: blue;
    }

    .login-check {
      width: 100%;
      display: flex;
      align-content: center;
      justify-content: space-between;
    }
  }
</style>
